﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Model.LiveControl;

namespace WindowApiConsle
{
    public class WindowApi
    {
        

        [DllImport("user32.dll")]
        public static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool GetWindowRect(IntPtr hWnd, ref Rect lpRect);

        //根据坐标获取窗口句柄 
        [DllImport("user32", EntryPoint = "WindowFromPoint")]
        public static extern IntPtr WindowFromPoint(int xPoint,int yPoint);

        [DllImport("user32.dll")]
        public static extern int GetWindowTextLength(IntPtr hWnd);

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int nMaxCount);

        private static Dictionary<int, Process> _dicProcesses= null; 
        /// <summary>
        ///     获取进程列表
        /// </summary>
        /// <returns>进程列表</returns>
        public static Process[] GetProcesses()
        {
            var processes = Process.GetProcesses();
            _dicProcesses = processes.ToDictionary(p => p.Id);
            return processes;
        }

        public static IdRect GetRectByPtr(IntPtr ptr)
        {
            var tRect = new Rect();
            GetWindowRect(ptr, ref tRect);
            return new IdRect()
            {
                Rect = tRect,
                Ptr = ptr.ToInt32(),
            };
        }

        /// <summary>
        ///     获取窗口外包矩形
        /// </summary>
        /// <param name="ptr">进程句柄</param>
        /// <returns>窗口矩形</returns>
        public static IdRect GetRect(int ptr)
        {
            var tRect = new Rect();
            GetWindowRect(new IntPtr(ptr), ref tRect);
            return new IdRect
            {
                Rect = tRect,
                Ptr = ptr
            };
        }

        public static RectList GetRects()
        {
            var tRectlist= new RectList();
            var count = _dicProcesses.Count;
            tRectlist.Rects = new IdRect[count];
            int index = 0;
            foreach (var dicProcess in _dicProcesses)
            {
                try
                {
                    tRectlist.Rects[index] = GetRect(dicProcess.Key);
                }
                catch (Exception)
                {
                    tRectlist.Rects[index] = new IdRect();
                    // ignored
                }
                index++;
            }
            for (var i = 0; i < count; i++)
            {
                
            }
            return tRectlist;
        }
        /// <summary>
        ///     获取进程列表信息
        /// </summary>
        /// <returns>进程列表信息</returns>
        public static ProccessInfoList GetProces()
        {
            var processes = GetProcesses();
            var tRects = new ProccessInfoList
            {
                ProccessInfos = new ProccessInfo[processes.Length]
            };
            var index = 0;
            foreach (var process in processes)
            {
                var tRect = new ProccessInfo
                {
                    Name = process.ProcessName,
                    Ptr = process.Handle.ToInt32()
                };
                tRects.ProccessInfos[index++] = tRect;
            }
            return tRects;
        }
    }
}